extends Node2D

# 暴风雪效果属性
@export var duration: float = 8.0  # 暴风雪持续时间
@export var damage_per_second: int = 2  # 每秒伤害
@export var slow_factor: float = 0.7  # 减速效果

var time_alive: float = 0.0
var damage_timer: float = 0.0
var max_scale: float = 3.5  # 最大缩放比例
var player = null

func _ready():
	# 设置初始缩放
	scale = Vector2(0.1, 0.1)
	
	# 获取玩家引用
	player = get_tree().get_first_node_in_group("player")
	
	# 播放音效（如果有）
	if has_node("AudioStreamPlayer2D"):
		$AudioStreamPlayer2D.play()
	
	# 设置粒子效果
	if has_node("CPUParticles2D"):
		$CPUParticles2D.emitting = true
		$CPUParticles2D.amount = 800
		$CPUParticles2D.lifetime = 2.0
		$CPUParticles2D.emission_shape = CPUParticles2D.EMISSION_SHAPE_SPHERE
		$CPUParticles2D.emission_sphere_radius = 200.0
		$CPUParticles2D.direction = Vector2(1, 0.5)
		$CPUParticles2D.spread = 30.0
		$CPUParticles2D.gravity = Vector2(0, 20)
		$CPUParticles2D.initial_velocity_min = 100
		$CPUParticles2D.initial_velocity_max = 150
		$CPUParticles2D.scale_amount = 2.0
		$CPUParticles2D.color = Color(1.0, 1.0, 1.0, 0.8)

func _process(delta):
	# 更新生命周期
	time_alive += delta
	
	# 计算当前缩放比例
	var current_scale_factor = 0.0
	
	if time_alive < duration * 0.2:
		# 前20%时间快速增大
		current_scale_factor = time_alive / (duration * 0.2) * max_scale
	elif time_alive > duration * 0.8:
		# 后20%时间缓慢减小
		current_scale_factor = (1.0 - (time_alive - duration * 0.8) / (duration * 0.2)) * max_scale
	else:
		# 中间60%时间保持最大尺寸
		current_scale_factor = max_scale
	
	# 应用缩放
	scale = Vector2(current_scale_factor, current_scale_factor)
	
	# 旋转效果
	rotation += 0.1 * delta
	
	# 伤害计时器
	damage_timer += delta
	if damage_timer >= 1.0:  # 每秒造成一次伤害
		damage_timer = 0.0
		apply_damage_to_player()
	
	# 到达最大存活时间后消失
	if time_alive >= duration:
		# 淡出效果
		if has_node("CPUParticles2D"):
			$CPUParticles2D.emitting = false
			await get_tree().create_timer(2.0).timeout  # 等待粒子消失
		queue_free()

func apply_damage_to_player():
	# 检查玩家是否在暴风雪范围内
	if player and is_instance_valid(player):
		var distance = global_position.distance_to(player.global_position)
		var effect_radius = 200.0 * scale.x / max_scale
		
		if distance <= effect_radius:
			# 对玩家造成伤害
			player.take_damage(damage_per_second)
			
			# 应用减速效果
			if player.has_method("apply_slow_effect"):
				player.apply_slow_effect(slow_factor, 1.5)
